﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_PictureNewsEdit : System.Web.UI.Page
{
    private int PictureNewsID;
    protected void Page_Load(object sender, EventArgs e)
    {

    //    Image HotNews_Picture = FindControl("HotNews_Picture") as Image;
        if (Session["admin"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else
        {
            if (Session["OperationType"].ToString().Equals("New"))
            {
                //HotNews_Picture.Visible = false;
                if (0 == (int)Session["GroupID"])
                {
                    PublishLabel.Visible = false;
                    cbPublish.Visible = false;
                }
            }
            else
            {
                //NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                //var News = from news in dc.News
                //           join news_to_catalog in dc.NewsToCatalogs
                //           on news.News_id equals news_to_catalog.News_id
                //           where news_to_catalog.Catalog_id.Equals(1)
                //           select news;

                //foreach (var news in News)
                //{
                //    PictureNewsID = Convert.ToInt32(news.News_id);
                //    content.Value = Convert.ToString(news.Content);
                //    title.Value = Convert.ToString(news.Title);
                //}

                PictureNewsID = Convert.ToInt32(Session["News_id"]);

                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var News = from news in dc.News
                           where news.News_id.Equals(PictureNewsID)
                           select new
                           {
                               标题 = news.Title,
                               内容 = news.Content,
                               发布 = news.isPublished
                           };

                foreach (var news in News)
                {
                    content.Value = Convert.ToString(news.内容);
                    title.Value = Convert.ToString(news.标题);
                    if (news.发布 == 1) cbPublish.Checked = true;
                }
                if (0 == (int)Session["GroupID"])
                {
                    PublishLabel.Visible = false;
                    cbPublish.Visible = false;
                }

                var picure = from picture in dc.Pictures
                             where picture.News_id.Equals(PictureNewsID)
                             select new 
                             {
                                 URL=picture.URL
                            };
                foreach (var pic in picure)
                {
                    //labelUpload.Text = "已上传文件";
                    HotNews_Picture.ImageUrl = pic.URL;
                    HotNews_Picture.DescriptionUrl = pic.URL;
                    HotNews_Picture.AlternateText = "已上传文件";
                    //HotNews_Picture.Visible = true;
                }

                dc.Dispose();

            }
        }

    }
  
    protected void Submit_Button_Click(object sender, EventArgs e)
    {
        string tableTitle = "ctl00$ContentPlaceHolder$title";
        string tableContent = "ctl00$ContentPlaceHolder$content";
        string tablePublish = "ctl00$ContentPlaceHolder$cbPublish";
        if (0 == (int)Session["GroupID"])
        {
            PublishLabel.Visible = false;
            cbPublish.Visible = false;
        }
        if (Session["OperationType"].ToString().Equals("New"))
        {

            if (Session["URL"] == null)
            {
                JavaScript.JSDialog.Alert(this, "您必须选择上传一张主题图片");
                return;
            }
//            string NewsType = Session["NewsType"].ToString().Replace("编辑", "");
            int catalog_id = Convert.ToInt32(Session["NewsCatalogID"].ToString());
            int news_id = 0;

             NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
//             var Catalog = from catalog in dc.Catalogs
//                           where catalog.CatalogName.Equals(NewsType)
//                           select catalog;
//             foreach (var catalog in Catalog)
//             {
//                 catalog_id = catalog.Catalog_id;
//             }


            News news = new News();
            news.Time = DateTime.Now;
            news.Editor = (int)Session["UserID"];
            news.Title = Request.Form[tableTitle];
            news.Content = Request.Form[tableContent];
            if (Request.Form[tablePublish] != null) { news.isPublished = 1; news.Checker = (int)Session["UserID"]; }
            else { news.isPublished = 0; news.Checker = 0; }

            try
            {
                SQL.SQLHelper.Insert(news);
            }
            catch (Exception ex)
            {
            }

            var News = from pNews in dc.News
                       where pNews.Title.Equals(Request.Form[tableTitle].ToString())
                       select pNews;
            foreach (var pNews in News)
            {
                news_id = pNews.News_id;
            }
            dc.Dispose();

            NewsToCatalog newsToCatalog = new NewsToCatalog();
            newsToCatalog.News_id = news_id;
            newsToCatalog.Catalog_id = catalog_id;

            try
            {
                SQL.SQLHelper.Insert(newsToCatalog);
            }
            catch (Exception ex)
            {
            }

            Picture picture = new Picture();
            picture.News_id = news_id;
            picture.URL = Convert.ToString(Session["URL"]);

            try
            {
                SQL.SQLHelper.Insert(picture);
            }
            catch (Exception ex)
            {
            }

            title.Value = Request.Form[tableTitle];
            content.Value = Request.Form[tableContent];
            if (Request.Form[tablePublish] != null) cbPublish.Checked = true;
            else cbPublish.Checked = false;
        }
        else if (Session["OperationType"].ToString().Equals("Edit"))
        {
            NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
            var News = from news in dc.News
                       where news.News_id.Equals(PictureNewsID)
                       select news;
            foreach (var news in News)
            {
                news.Title = Request.Form[tableTitle];
                news.Content = Request.Form[tableContent];
                if (Request.Form[tablePublish] != null)
                {
                    news.isPublished = 1;
                    if (news.Checker == 0) news.Checker = (int)Session["UserID"];
                }
                else { news.isPublished = 0; news.Checker = 0; }
            }
            title.Value = Request.Form[tableTitle];
            content.Value = Request.Form[tableContent];
            if (Request.Form[tablePublish] != null) cbPublish.Checked = true;
            else cbPublish.Checked = false;

            dc.SubmitChanges();
            dc.Dispose();

        }
        Session.Remove("URL");
        Response.Redirect("../Admin/AdminHome.aspx");
    }
    protected void Upload_Click(object sender, EventArgs e)
    {
        if (FileUpLoad.HasFile)
        {
            string fileContentType = FileUpLoad.PostedFile.ContentType;

            FileInfo File = new FileInfo(FileUpLoad.PostedFile.FileName);
            string FileName = File.Name;
            string WebFilePath = Server.MapPath("../images/" + FileName);
            try
            {
                FileUpLoad.SaveAs(WebFilePath);
                HotNews_Picture.DescriptionUrl =HotNews_Picture.ImageUrl = "../images/" + FileName;
                HotNews_Picture.AlternateText = "已上传文件";
                //HotNews_Picture.Visible = true;
            }
            catch (Exception ex)
            {
                JavaScript.JSDialog.Alert(this, "提示：文件上传失败，失败原因：" + ex.Message);
            }

            if (Session["OperationType"].ToString().Equals("New"))
            {
                Session.Add("URL", "~/images/" + FileName);
                //labelUpload.Text = "已上传文件";
            }
            else
            {
                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var Picture = from picture in dc.Pictures
                              where picture.News_id.Equals(PictureNewsID)
                              select picture;
                foreach (var picture in Picture)
                {
                    picture.URL = "~/images/" + FileName;
                }
                dc.SubmitChanges();
                dc.Dispose();
                title.Value = Request.Form["title"];
                content.Value = Request.Form["content"];
                //labelUpload.Text = "已上传文件";
            }
        }
        else
        {

        }
    }
    protected void Return_Button_Click(object sender, EventArgs e)
    {
        this.RegisterClientScriptBlock("E", "<script language=javascript>history.go(-2);</script>");
    }
}
